package com.gmgx.hrms.dao.impl;

import com.gmgx.hrms.dao.RoleDao;
import com.gmgx.hrms.entity.Role;
import com.gmgx.hrms.util.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    private JDBCTools tools=new JDBCTools();
    @Override
    public Role queryRoleById(Integer id) {
        Role role=null;
       ResultSet rs= tools.query("select * from t_role where role_id=?",id);
        try {
            if(rs.next()){
                role=new Role();
                role.setRole_id(rs.getInt("role_id"));
                role.setRole_level(rs.getInt("role_level"));
                role.setRole_name(rs.getString("role_name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return role;
    }

    @Override
    public List<Role> queryAllRole() {
        List<Role> list=new ArrayList<>();
        ResultSet rs= tools.query("select * from t_role");
        try {
            while(rs.next()){
                Role role=new Role();
                role.setRole_id(rs.getInt("role_id"));
                role.setRole_level(rs.getInt("role_level"));
                role.setRole_name(rs.getString("role_name"));
                list.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }
}
